package com.learn.thread.demo03;

/**
 * 线程中断，基于Thread实现
 * @author Java丶小明
 * @date 2021/6/25 16:24
 */
public class PrimeGenerator01 extends Thread {


    @Override
    public void run() {
        long number = 1L;
        while (true) {
            if (isPrime(number)) {
                System.out.printf("Number %d is Prime", number);
            }
            //如果线程中断了，则线程运行结束
            if (isInterrupted()){
                System.out.println("\nThe Prime Generator has been Interrupted");
                return;
            }
            number++;
        }
    }

    /**
     * 获取质数
     * @param number 数
     * @return 质数
     */
    private boolean isPrime(long number) {
        if (number <= 2) {
            return true;
        }
        for (long i = 2; i < number; i++) {
            if ((number % i) == 0) {
                return false;
            }
        }
        return true;
    }
}
